package com.attilax.util;

import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//import org.jfree.chart.ChartFactory;












import com.attilax.Closure;

import m.dirPkg.travDir;

public class dirx {

	public static List<String> getFiles(String strPath) {
		travDir.refreshFileList(strPath);
		List<String> li = travDir.filelist;
		
		return li;
	}

	/**
	@author attilax ���۵�צ��
		@since  2014-5-27 ����02:42:43$
	
	 * @param s
	 * @return
	 */
	public static String getParentPath(String s) {
		// attilax ���۵�צ��  ����02:42:43   2014-5-27 
		File f=new File(s); 
		return f.getParent();
	}
	/**
	 * 
	@author attilax 老哇的爪子
	\t@since  Aug 2, 2014 3:40:56 AM$
	
	 * @param strPath
	 */
	public static void trave(String strPath ,Closure closure) { 
	    File dir = new File(strPath); 
	    if(!dir.exists())
	    	throw new RuntimeException("no this dir:"+strPath);
	    File[] files = dir.listFiles(); 
	    
	    if (files == null) 
	        return; 
	    int length = files.length;
		for (int i = 0; i < length; i++) { 
	        if (files[i].isDirectory()) { 
	        	trave(files[i].getAbsolutePath(),closure); 
	        } else { 
	            String strFileName = files[i].getAbsolutePath().toLowerCase();
	            
	            try {
					closure.execute(files[i].getAbsolutePath());
				} catch (Exception e) {
					e.printStackTrace();
				}  
	            
	        } 
	    } 
	}
	
	
	/**  
	 *  not safe ver
	@author attilax 老哇的爪子
	\t@since  Aug 2, 2014 3:40:56 AM$
	
	 * @param strPath
	 */
	public static void trave_NS(String strPath ,Closure closure) { 
	    File dir = new File(strPath); 
	    File[] files = dir.listFiles(); 
	    
	    if (files == null) 
	        return; 
	    for (int i = 0; i < files.length; i++) { 
	        if (files[i].isDirectory()) { 
	        	trave(files[i].getAbsolutePath(),closure); 
	        } else { 
	            String strFileName = files[i].getAbsolutePath().toLowerCase();
	            
	            try {
					closure.execute(files[i].getAbsolutePath());
				} catch (Exception e) {
					throw  new RuntimeException("",e);
				}  
	            
	        } 
	    } 
	}

		public static void trave_files_noIncludeDir(String dir, Closure closure) {
		trave(dir,closure);
		
	}

		public static List traveASListOrderbyPath_wzDir(String dir) {
			 final List li=new ArrayList();
			 dirx.trave_all( dir, new Closure() {

					@Override
					public Object execute(Object arg0) throws Exception {
						String p = (String) arg0;
						
					 li.add(p);

						// System.out.println("move \""+dir +"\" e:\\0");

						// System.out.println(i++);
						return null;
					}
				});
			return sort( li);
		}
		public static void trave_all(String strPath ,Closure closure) { 
			
			   
	        try {
				closure.execute(strPath);
			} catch (Exception e) {
				e.printStackTrace();
			}  
		    File dir = new File(strPath); 
		    if(!dir.exists())
		    	throw new RuntimeException("no this dir:"+strPath);
		    File[] files = dir.listFiles(); 
		    
		    if (files == null) 
		        return; 
		    int length = files.length;
			for (int i = 0; i < length; i++) { 
				try {
					 if (files[i].isDirectory()) { 
				        	trave_all(files[i].getAbsolutePath(),closure); 
				        } else { 
				            String strFileName = files[i].getAbsolutePath().toLowerCase();
				            
				            try {
								closure.execute(files[i].getAbsolutePath());
							} catch (Exception e) {
								e.printStackTrace();
							}  
				            
				        } 
				} catch (Exception e) {
					e.printStackTrace();
				}
		       
		    } 
		}


		private static List sort(List li) {
			Collections.sort(li,Collator.getInstance(java.util.Locale.CHINA));
			return li;
		}
}
